package cn.edu.zjut.medlinebackend.service.impl;
import java.util.Date;
import java.util.HashMap;

import cn.edu.zjut.medlinebackend.model.Post;
import cn.edu.zjut.medlinebackend.model.dto.PictureEsDTO;
import cn.edu.zjut.medlinebackend.model.dto.PostEsDTO;
import cn.edu.zjut.medlinebackend.model.vo.PostVO;
import cn.edu.zjut.medlinebackend.service.PostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.zjut.medlinebackend.model.Picture;
import cn.edu.zjut.medlinebackend.service.PictureService;
import cn.edu.zjut.medlinebackend.mapper.PictureMapper;
import com.google.gson.Gson;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86182
* @description 针对表【picture】的数据库操作Service实现
* @createDate 2024-01-01 21:19:18
*/
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture>
    implements PictureService{
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public List<PictureEsDTO> getPicFromEs(String keyWord, Long fileId) {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 过滤
        boolQueryBuilder.filter(QueryBuilders.termQuery("isDelete", 0));
        boolQueryBuilder.filter(QueryBuilders.termQuery("fileId", fileId));

        // 按关键词检索
        if (org.apache.commons.lang3.StringUtils.isNotBlank(keyWord)) {
            boolQueryBuilder.should(QueryBuilders.matchQuery("content", keyWord));
            boolQueryBuilder.minimumShouldMatch(1);
        }

        // 构造查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder)
                .withHighlightFields(new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>"))
                .build();
        SearchHits<PictureEsDTO> searchHits = elasticsearchRestTemplate.search(searchQuery, PictureEsDTO.class);

        System.out.println(searchHits);
        List<PictureEsDTO> resultList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());

        if(resultList.isEmpty()){
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .should(QueryBuilders.wildcardQuery("content", "*" + keyWord + "*"));

            searchQuery = new NativeSearchQuery(queryBuilder);
            searchHits = elasticsearchRestTemplate.search(searchQuery, PictureEsDTO.class);
            resultList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());

            resultList = resultList.stream().filter(picture -> picture.getFileId().equals(fileId)).collect(Collectors.toList());
        }

        return resultList;
    }

    @Override
    public List<PictureEsDTO> getAllPicFromEs(String keyWord) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 过滤
        boolQueryBuilder.filter(QueryBuilders.termQuery("isDelete", 0));

        // 按关键词检索
        if (org.apache.commons.lang3.StringUtils.isNotBlank(keyWord)) {
            boolQueryBuilder.should(QueryBuilders.matchQuery("content", keyWord));
            boolQueryBuilder.minimumShouldMatch(1);
        }

        // 构造查询
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder)
                .withHighlightFields(new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>"))
                .build();
        SearchHits<PictureEsDTO> searchHits = elasticsearchRestTemplate.search(searchQuery, PictureEsDTO.class);

        System.out.println(searchHits);
        List<PictureEsDTO> resultList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());

        if(resultList.isEmpty()){
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .should(QueryBuilders.wildcardQuery("content", "*" + keyWord + "*"));

            searchQuery = new NativeSearchQuery(queryBuilder);
            searchHits = elasticsearchRestTemplate.search(searchQuery, PictureEsDTO.class);
            resultList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        }

        return resultList;
    }


}




